package cn.chenxinjie.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author 陈鑫杰
 * @date 2022/3/21 15:43
 */
@Configuration
@EnableWebSecurity // 开启web安全
public class HrmWebSecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * Http网络安全
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //.antMatchers(HttpMethod.POST,"/hrm/login","/login","/hrm/refreshToken").permitAll() // 放行登陆请求
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated() // 拦截其他所有请求
                .and().formLogin().permitAll() // 开启表单登陆
                //.successForwardUrl("/success") // 登陆成功后的跳转页面
                .and().logout().permitAll()
                .and().csrf().disable();

    }

    /**
     * 设置密码加密编译
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
